/**
 * Copyright 2010 upTick Pty Ltd
 *
 * Licensed under the terms of the GNU General Public License version 3 
 * as published by the Free Software Foundation. You may obtain a copy of the
 * License at: http://www.gnu.org/copyleft/gpl.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations 
 * under the License.
 */

package au.com.uptick.serendipity.client.sales.account.presenter;

import au.com.uptick.serendipity.client.NameTokens;
import au.com.uptick.serendipity.client.Serendipity;
import au.com.uptick.serendipity.client.sales.account.view.handlers.AccountUiHandlers;
import au.com.uptick.serendipity.client.widgets.NavigationPane;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.HasUiHandlers;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ContentSlot;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.proxy.*;

public class AccountPresenter extends
                              Presenter<AccountPresenter.MyView, AccountPresenter.MyProxy> implements
                                                                                           AccountUiHandlers {

    private final PlaceManager placeManager;

    private static NavigationPane navigationPane;
    private static Activity activity;
    private static Long accountId;

    @ProxyStandard
    @NameToken(NameTokens.accountPage)
    public interface MyProxy extends Proxy<AccountPresenter>, Place {
    }

    public interface MyView extends View, HasUiHandlers<AccountUiHandlers> {
        NavigationPane getNavigationPane();
    }

    /**
     * Use this in leaf presenters, inside their {@link #revealInParent} method.
     */
    @ContentSlot
    public static final Type<RevealContentHandler<?>> TYPE_SetContextAreaContent = new Type<RevealContentHandler<?>>();

    @Inject
    public AccountPresenter(EventBus eventBus, MyView view, MyProxy proxy,
                            PlaceManager placeManager) {
        super(eventBus, view, proxy);

        getView().setUiHandlers(this);

        this.placeManager = placeManager;

        AccountPresenter.navigationPane = view.getNavigationPane();
        AccountPresenter.activity = Activity.NEW;
        AccountPresenter.accountId = -1L;
    }

    @Override
    protected void onBind() {
        super.onBind();

        Log.debug("onBind()");

        // expand the first Navigation Pane section
        getView().getNavigationPane().expandSection(Serendipity.getConstants().AccountDetailsStackSectionName());

        // reveal the first nested Presenter
        PlaceRequest placeRequest = new PlaceRequest(NameTokens.accountInformation);
        placeManager.revealPlace(placeRequest);
    }

    @Override
    protected void onReveal() {
        super.onReveal();
    }

    @Override
    protected void onReset() {
        super.onReset();
    }

    public enum Activity {
        INVALID_ACTIVITY, NEW, EDIT;
    }

    private static final String ACTIVITY = "activity";
    private static final String DEFAULT_ACTIVITY = "edit";
    private static final String EDIT = "edit";
    private static final String NEW = "new";
    private static final String ACCOUNT_ID = "accountId";

    @Override
    public void prepareFromRequest(PlaceRequest placeRequest) {
        super.prepareFromRequest(placeRequest);

        String activityString = placeRequest.getParameter(ACTIVITY, DEFAULT_ACTIVITY);
        activity = Activity.INVALID_ACTIVITY;

        if (activityString.equals(EDIT)) {
            activity = Activity.EDIT;
        } else if (activityString.equals(NEW)) {
            activity = Activity.NEW;
        }

        try {
            accountId = Long.valueOf(placeRequest.getParameter(ACCOUNT_ID, null));
        } catch (NumberFormatException e) {
            accountId = -1L;
        }

        Log.debug(activityString + ": " + accountId);

        if (activity == Activity.INVALID_ACTIVITY ||
            accountId == -1L && activity != Activity.NEW) {
            // placeManager.revealErrorPlace(placeRequest.getNameToken());
            Log.debug("activity == Activity.INVALID_ACTIVITY");
        }
    }

    @Override
    protected void revealInParent() {
        RevealRootContentEvent.fire(this, this);
    }

    native public static void close()/*-{
        $wnd.close();
    }-*/;

    public void onSaveAndCloseButtonClicked() {
        close();
    }

    public static NavigationPane getNavigationPane() {
        return navigationPane;
    }

    public static Activity getActivity() {
        return activity;
    }

    public static Long getAccountId() {
        return accountId;
    }
}